const formatTime = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

//格式化时间
function formatTimeThisMonth(date, isStart) {
  var year = date.getFullYear()
  var month = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();

  if (isStart == true) {
    return year + '-' + month + '-' + '01'
  }
  return year + '-' + month + '-' + day
}

var formatDay = function(datastr) {
	if (!datastr) return "";
	var date = new Date(datastr);
	return frmatDate(date);
}

const frmatStrDate = datastr => {
	var date = Date.parse(datastr);
	return frmatDate(date);
}

const frmatDate = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()

	return [year, month, day].map(formatNumber).join('-')
}

const addDateSeg = function(currDate, dateType, num, seg) {
	currDate = new Date(parseInt(currDate));
	var year;
	var month;
	var day;
	if (dateType == "Year") {
		year = currDate.getFullYear() + num;
		month = currDate.getMonth() + 1
		day = currDate.getDate();
	} else if (dateType == "Month") {
		year = currDate.getFullYear();
		month = currDate.getMonth() + 1 + num;
		day = currDate.getDate();
	} else if (dateType == "day") {
		year = currDate.getFullYear();
		month = currDate.getMonth() + 1;
		day = currDate.getDate() + num;
	}
	return [year, month, day].map(formatNumber).join(seg);
}


const addDate = function(currDate, dateType, num) {
	currDate = new Date(parseInt(currDate));
	var year;
	var month;
	var day;
	if (dateType == "Year") {
		year = currDate.getFullYear() + num;
		month = currDate.getMonth() + 1
		day = currDate.getDate();
	} else if (dateType == "Month") {
		year = currDate.getFullYear();
		month = currDate.getMonth() + 1 + num;
		day = currDate.getDate();
	} else if (dateType == "day") {
		year = currDate.getFullYear();
		month = currDate.getMonth() + 1;
		day = currDate.getDate() + num;
	}
	return Date.parse([year, month, day].map(formatNumber).join('/'));
}



const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : '0' + n
}

/**
 * 获取当月的一号和当天的日期的时间段
 */
function getCurrentMonthDate() {
	var date = new Date(); //获取系统当前时间
	var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
	var month = date.getMonth() + 1; //获取当前月份(0-11,0代表1月)
	var formatMonth = month > 9 ? month : "0" + month;
	var day = date.getDate(); //获取当前日(1-31)
	var formatDay = day > 9 ? day : "0" + day;
	return {
		starttime: year + "-" + formatMonth + "-01",
		endtime: year + "-" + formatMonth + "-" + formatDay
	};
}
/**
 * 获取当月的一号和当月的最后一天
 */
function getMonthDate() {
	var date = new Date(); //获取系统当前时间
	var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
	var month = date.getMonth() + 1; //获取当前月份(0-11,0代表1月)
	var formatMonth = month > 9 ? month : "0" + month;
	//var day = date.getDate(); //获取当前日(1-31)
	//var formatDay = day > 9 ? day : "0" + day;

	var nextMonthFirstDay = new Date(year, month, 1);
	var day = new Date(nextMonthFirstDay - 1000 * 60 * 60 * 24).getDate();
	var formatDay = day > 9 ? day : "0" + day;
	return {
		starttime: year + "-" + formatMonth + "-01",
		endtime: year + "-" + formatMonth + "-" + formatDay
	};
}

//角色检查
const checkRole = (mobilerole, roleIds, callback) => {
	if (mobilerole) {
		let isRole = false;
		for (let index in roleIds) {
			if (mobilerole.indexOf(roleIds[index]) != -1) {
				isRole = true;
				break;
			}
		}
		if (isRole) {

		} else {
			callback();
		}
	} else {
		callback();
	}
}

const haveRole = (mobilerole, roleIds) => {
	if (mobilerole) {
    let mobileroles = mobilerole.split('|');
    for (var i = 0; i < mobileroles.length;i++){
      for(var j=0;j<roleIds.length;j++){
        if (mobileroles[i]==roleIds[j])
           return true;
      }
    }
	}
  return false;
}

//判断字符串是否为数字
const checkNum = (inputNum) => {
	var re = /^[0-9]+.?[0-9]*$/;
	if (!re.test(inputNum)) {
		return false;
	} else {
		return true;
	}
}
//判断是否为手机
const checkPhone = (phone) => {
	var regPhoneNum = /^1(3|4|5|6|7|8|9)\d{9}$/;
	if (!regPhoneNum.test(phone)) {
		return false;
	} else {
		return true;
	}
}
//提示
const shouTips = () => {
	wx.showToast({
		title: '请输入数字',
		icon: 'none',
		duration: 2000
	});
}

const getDefaultDate = (date, hour, minute, second) => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

//根据出生日期计算年龄
const getAgeBybirthDay = (birthday) => {
	if (birthday) {
		//出生时间 毫秒
		var birthDayTime = new Date(birthday).getTime();
		//当前时间 毫秒
		var nowTime = new Date().getTime();
		//一年毫秒数(365 * 86400000 = 31536000000)
		return Math.floor((nowTime - birthDayTime) / 31536000000);
	}
	return "";
}

/**
 * 处理字符串为****格式，中间显示四个*号
 * str 需要处理的字符串
 * startLength 前面显示的字符串长度
 * endLength 后面显示的字符串长度
 */
const replaceIDPhone = function(str, startLength, endLength) {
	if (str == undefined || str.length == 0) {
		return "";
	}
	var length = str.length;
	if (length >= startLength + endLength) {
		return str.substring(0, startLength) + "****" + str.substring(length - endLength, length);
	} else {
		return str;
	}
}

module.exports = {
	addDateSeg: addDateSeg,
	formatTime: formatTime,
	frmatStrDate: frmatStrDate,
	addDate: addDate,
	getCurrentMonthDate: getCurrentMonthDate,
	getMonthDate: getMonthDate,
	formatDay: formatDay,
	frmatDate: frmatDate,
	checkRole: checkRole,
	haveRole: haveRole,
	shouTips: shouTips,
	checkPhone: checkPhone,
	checkNum: checkNum,
	getDefaultDate: getDefaultDate,
	getAgeBybirthDay: getAgeBybirthDay,
	replaceIDPhone: replaceIDPhone,
  formatTimeThisMonth: formatTimeThisMonth
}
